package com.example.cameraglrender.filter;

import android.content.Context;
import android.opengl.GLES20;

import com.example.cameraglrender.R;

public class FBO2SoulFilter extends AbstractFBOFilter {

    //顶点程序
    private int mixturePercent;
    float scale = 0.0f; //缩放，越大就放的越大
    //     句柄
    private int scalePercent;
    float mix = 0.0f; //透明度，越大越透明



    public FBO2SoulFilter(Context mContext) {
        super(mContext, R.raw.fbo2_soul_vertex, R.raw.fbo2_soul_frag);
        scalePercent = GLES20.glGetUniformLocation(mProgramId, "scalePercent");
        mixturePercent = GLES20.glGetUniformLocation(mProgramId, "mixturePercent");
    }


    @Override
    public int onDrawFrame(int textureId) {

        scale += 0.08f;
        mix += 0.08f;
        if (scale >= 1.0) {
            scale = 0.0f;
        }
        if (mix >= 1.0) {
            mix = 0.0f;
        }

        GLES20.glViewport(0, 0, mOutputWidth, mOutputHeight);

        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, mFrameBuffers[0]);
        GLES20.glUseProgram(mProgramId);


        //赋值vPosition
        mGlVertexBuffer.position(0);
        GLES20.glVertexAttribPointer(vPosition, 2, GLES20.GL_FLOAT, false, 0, mGlVertexBuffer);
        GLES20.glEnableVertexAttribArray(vPosition);

        //赋值vCoord
        mGlTextureBuffer.position(0);
        GLES20.glVertexAttribPointer(vCoord, 2, GLES20.GL_FLOAT, false, 0, mGlTextureBuffer);
        GLES20.glEnableVertexAttribArray(vCoord);

        GLES20.glActiveTexture(GLES20.GL_TEXTURE0);

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
        //赋值vTexture
        GLES20.glUniform1i(vTexture, 0);

        GLES20.glUniform1f(scalePercent, scale + 1);
        GLES20.glUniform1f(mixturePercent, 1.0f - mix);

        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);

        GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
        GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);

        return mFBOTextures[0];
    }

}
